<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<?php
$list["l.time"] = "DESC";
$list["l.type_money"] = "DESC";
$list["l.type_trade"] = "DESC";
$list["l.value"] = "DESC";
$list["e.FullName"] = "DESC";
$str = "l.time";
$_SESSION['sortn'] = $str;
$_SESSION['sort'] = $list;


unset($_SESSION['searchMoney']);
require_once('calendar/classes/tc_calendar.php');
$fromdate = date("Y-m-d");
$todate = date("Y-m-d");
$query = mysql_query("select * from manage_money");
$logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 ORDER BY " . $str . " " . $list[$str] . ',id DESC');

$page = 1;
if (!isset($_REQUEST['page'])) {
    $page = 1;
} else {
    $page = $_REQUEST['page'];
}
$rows_per_page = 10;
$page_start = ( $page - 1 ) * $rows_per_page;
$page_end = $page * $rows_per_page;
$number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);

$myCalendar = new tc_calendar("date", true);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$d = split('-', $todate, 3);

$myCalendar->setDate($d[2], $d[1], $d[0]);
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(2010, 2015);
$myCalendar->dateAllow('2010-01-01', '2015-12-31');
//$myCalendar1->setHeight(350);
//$myCalendar1->autoSubmit(true, "form1");
//$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
$myCalendar->setOnChange("myChanged('test')");
//$myCalendar->rtl = true;
$addressbrowser = $_SERVER['SERVER_NAME'];
$temp = explode(".", $addressbrowser);

$queryDepartment = mysql_query("SELECT d.IDDepartment,dl.DepartmentName FROM department d,department_lang dl WHERE d.IDDepartment=dl.IDDepartment AND dl.LangID='$lang';");
$querySendMail = mysql_query("SELECT e.IDEmployee,e.FullName FROM list_sendmailmoney l,employee e WHERE  e.IDEmployee=l.employee_id GROUP BY l.employee_id");

include 'popup/popup_sendmailMoney.php';
?>
<script language='javascript'>
    <!--
    $(document).ready(function() {
        $('#vMoney').priceFormat({
            prefix: '',
            centsLimit: 0,
            centsSeparator: '',
            thousandsSeparator: ','
        });
    });
    function isNumberKey(evt)
    {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
    function save()
    {
        var day=document.getElementById("date_day").value;
        var month=document.getElementById("date_month").value;
        var year=document.getElementById("date_year").value;
        var time=year+"-"+month+"-"+day;
        var vMoney=document.getElementById("vMoney").value;
        var tMoney=document.getElementById("tMoney").value;
        var mNote=document.getElementById("mNote").value;
        var tTrade=0;
        //alert(vMoney+tMoney+tTrade);
        //if(vMoney!=0){
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp2=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp2.onreadystatechange=function(){
            if (xmlhttp2.readyState==4 && xmlhttp2.status==200){
											  	
                document.getElementById("frmMoney").innerHTML=xmlhttp2.responseText;
                var kq=document.getElementById("kq").value;
                if(kq==1) alert("Unsuccessful");
                else {
                    document.getElementById("vMoney").value=0;
                    document.getElementById("tMoney").value=0;
                    //document.getElementById("tTrade").value=0;
                    document.getElementById("mNote").value="";
                    onLoadLogMoney();
                }
            }
        }
        xmlhttp2.open("POST","content_money/saveMoney.php?vMoney="+vMoney+"&tMoney="+tMoney+"&tTrade="+tTrade+"&time="+time+"&mNote="+encodeURIComponent(mNote),true);
        xmlhttp2.send();
        //}else{
	
        //}
    }
    function onLoadLogMoney()
    {
        var day1=document.getElementById("date1_day").value;
        var month1=document.getElementById("date1_month").value;
        var year1=document.getElementById("date1_year").value;
        var time1=year1+"-"+month1+"-"+day1;

        var day2=document.getElementById("date2_day").value;
        var month2=document.getElementById("date2_month").value;
        var year2=document.getElementById("date2_year").value;
        var time2=year2+"-"+month2+"-"+day2;
        var page=1;
        var str1="searchAll";
        var str="l.time";
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp2=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp2.onreadystatechange=function(){
            if (xmlhttp2.readyState==4 && xmlhttp2.status==200){
                document.getElementById("frmLogMoney").innerHTML=xmlhttp2.responseText;
                alert("Successful")
            }
        }
        xmlhttp2.open("POST","content_money/LoadLogMoney.php?action="+str1+"&fromdate="+time1+"&todate="+time2+"&page="+page+"&str="+str+"&co=0",true);
        xmlhttp2.send();
    }
    function sortMoney(page,str,co,str1){
	
        var day1=document.getElementById("date1_day").value;
        var month1=document.getElementById("date1_month").value;
        var year1=document.getElementById("date1_year").value;
        var time1=year1+"-"+month1+"-"+day1;

        var day2=document.getElementById("date2_day").value;
        var month2=document.getElementById("date2_month").value;
        var year2=document.getElementById("date2_year").value;
        var time2=year2+"-"+month2+"-"+day2;

        //var sTrade=document.getElementById("tsTrade").value;
        //alert(time1+"   "+ time2);

        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp2=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp2.onreadystatechange=function(){
            if (xmlhttp2.readyState==4 && xmlhttp2.status==200){
                document.getElementById("frmLogMoney").innerHTML=xmlhttp2.responseText;
										    	
            }
        }
        //if(str1=='search')
        //	xmlhttp2.open("POST","content_money/LoadLogMoney.php?action="+str1+"&fromdate="+time1+"&todate="+time2+"&page="+page+"&str="+str+"&co="+co+"&sTrade="+sTrade,true);
        //else
        xmlhttp2.open("POST","content_money/LoadLogMoney.php?action="+str1+"&fromdate="+time1+"&todate="+time2+"&page="+page+"&str="+str+"&co="+co,true);
        xmlhttp2.send();
    }
    /*function sendmail(lang,company){
		
	    
		
		
               //alert(lang+company);
		
               if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                       xmlhttp=new XMLHttpRequest();
               }else{// code for IE6, IE5
                       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
               }
               xmlhttp.onreadystatechange=function(){
                                                                                         if (xmlhttp.readyState==4 && xmlhttp.status==200){
                                                                                                       //document.getElementById('showview').setAttribute('style','opacity:1; position:relative;');
                                                                                               alert("Send mail successful");
                                                                                         }
                                                                                 }
               xmlhttp.open("GET","content_money/sendmail_money.php?lang="+lang+"&company="+company,true);
		
               xmlhttp.send();
       }*/
    function sendmail(){
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                //document.getElementById('showview').setAttribute('style','opacity:1; position:relative;');
                alert("Update Send mail successful");
            }
        }
        xmlhttp.open("GET","content_money/sendmail.php",true);
		
        xmlhttp.send();
    }
    function showsendmailMoney(){
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=60)'}).fadeIn(); //Fade in the fade layer       
		
        popup_window_show('#sample', { pos : 'window-center', width : '600px', height:'800px'});
											
		
    }
    function showUser1(str){
        if (str==""){
            document.getElementById("txtHint").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","ajax/getuser1.php?q="+str,true);
        xmlhttp.send();
    }
    function showReference1(str){ 
        if (str==""){
            document.getElementById("txtHint1").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","ajax/getreference4.php?idemployee="+str,true);
        xmlhttp.send();
    }
    function rmReference1(str){ 
        if (str==""){
            document.getElementById("txtHint1").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","ajax/rmreference4.php?idemployee="+str,true);
        xmlhttp.send();
    }
    function sendmail(){
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                //document.getElementById('showview').setAttribute('style','opacity:1; position:relative;');
                alert("Update Send mail successful");
            }
        }
        xmlhttp.open("GET","content_money/sendmail.php",true);
		
        xmlhttp.send();
    }
    //-->
</script>
<div id="contains" style="height:100%;">
    <div class="title" style="padding-top: 5px;font-weight: bold;"><?= $l_manageMoney ?></div>
    <div class="content_code" style="height:100%;">
        <div class="approval-right" style="padding-right:10px; height:100%;">

            <?= $logMoney1 ?><br>
            <div  style="width:100%;border: 1px solid silver; padding: 10px 0; " id="frmMoney">

                <table style="width:100%; padding-left:10px;">
                    <?php
                    $tong = 0;
                    while ($row = mysql_fetch_array($query)) {
                        $tong+=$row['value'];
                        ?>
                        <tr>
                            <td  class="tdfirst"><?= $row['name' . $lang] ?></td>
                            <td style="width: 260px"><input type="text" id="txttime" name="txttime" value="<?= number_format($row['value']) ?>" class="in_testfield" style="width:185px;float:left;height:33px;text-align: right;" readonly="readonly"/>  <p style="padding-left: 200px;">  VNĐ</p></td>
                            <td></td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td  class="tdfirst"><?= $l_total ?></td>
                        <td style="width: 260px"><input type="text" id="txttime" name="txttime" value="<?= number_format($tong) ?>" class="in_testfield" style="width:185px;float:left;height:33px;text-align: right;" readonly="readonly"/>  <p style="padding-left: 200px;">  VNĐ</p></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center;"><input style="margin:12px 5px 0 0; font-size: 12px;" type="button" value="<?= $l_sendmail ?>" onclick="showsendmailMoney()"  class="btn_choose"  /></td>
                        <td></td>
                    </tr>
                </table>

            </div>
            <br><br>
            <?= $logMoney2 ?><br>
            <div  style="width:100%;border: 1px solid silver; ">

                <table style="width:100%; padding-left:10px;">

<!--  <tr >
        <td  class="tdfirst"><?= $l_nhap ?>/<?= $l_xuat ?></td>
        <td style="width: 260px">
                <select name="tTrade" id="tTrade" class="in_testfield" style="width:185px;float:left;height:33px;">
										<option value="0"><?= $l_nhap ?></option>
										<option value="1"><?= $l_xuat ?></option>
									</select>
								</td>
        <td ></td>
							</tr>-->
                    <tr style="display: none" >
                        <td  class="tdfirst"><?= $l_time ?></td>
                        <td style="width: 260px"><?php $myCalendar->writeScript(); ?></td>
                        <td ></td>
                    </tr>
                    <tr >
                        <td  class="tdfirst"><?= $l_tMoney ?></td>
                        <td style="width: 260px">
                            <select name="tMoney" id="tMoney" class="in_testfield" style="width:185px;float:left;height:33px;">
                                <option value="0"><?= $l_tk1 ?></option>
                                <option value="1"><?= $l_tk2 ?></option>
                            </select>
                        </td>
                        <td></td>
                    </tr>

                    <tr >
                        <td  class="tdfirst"><?= $l_money ?></td>
                        <td><input type="text" name="vMoney" id="vMoney" onKeyPress="return isNumberKey(event)"  class="in_testfield" style="width:185px;float:left;height:33px;text-align: right;" value='0'/>  <p style="padding-left: 200px;">  VNĐ</p></td>
                        <td></td>
                    </tr>
                    <tr >
                        <td  class="tdfirst"><?= $l_note ?></td>
                        <td><input type="text" name="mNote" id="mNote"  class="in_testfield" style="width:185px;float:left;height:33px;text-align: right;" /></td>
                        <td ><input style="margin:12px 5px 0 0; font-size: 12px;" type="button" value="<?= $l_xacnhan ?>" onclick="save();"  class="btn_choose"  /></td>
                    </tr>

                </table>	



            </div>

            <br><br>
            <?= $logMoney3 ?>
            <br>
            <div  style="width:100%;border: 1px solid silver;padding: 10px 0"  >
                <table  cellpadding="0" cellspacing="1" border="0" class="ftable1" style="padding-left: 10px">
                                                                                        <!-- <tr height="40">
                                                                                        <td align="center" style="vertical-align:middle;width: 120px"><?php echo $l_nhap . '/' . $l_xuat; ?></td>
                                                                                        <td colspan="2"> 
                                                                                                <select name="tTrade" id="tsTrade" class="in_testfield" style="width:185px;float:left;height:33px;">
														<option value="-1"><?= $l_all ?></option>
														<option value="0"><?= $l_nhap ?></option>
														<option value="1"><?= $l_xuat ?></option>
													</select>
												</td>
                                                                                        <td rowspan="2"></td>
                                                                                                
												</tr> -->
                    <tr height="40">
                        <td align="center" style="vertical-align:middle;width: 120px"><?= $l_created_date ?></td>
                        <td align="center" style="height:24px;width: 280px;">
                            <?php
                            $myCalendar1 = new tc_calendar("date1", true);
                            $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
                            $d = split('-', $fromdate, 3);

                            $myCalendar1->setDate($d[2], $d[1], $d[0]);
                            $myCalendar1->setPath("calendar/");
                            $myCalendar1->setYearInterval(2010, 2015);
                            $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
//$myCalendar1->setHeight(350);
//$myCalendar1->autoSubmit(true, "form1");
//$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
                            $myCalendar1->setOnChange("myChanged('test')");
//$myCalendar->rtl = true;
                            $myCalendar1->writeScript();

//echo $myCalendar1->getDate();
                            ?>
                        </td >

                        <td align="center" style="height:24px;width: 280px;">
                            <?php
                            $myCalendar2 = new tc_calendar("date2", true);
                            $myCalendar2->setIcon("calendar/images/iconCalendar.gif");
                            $d = split('-', $todate, 3);

                            $myCalendar2->setDate($d[2], $d[1], $d[0]);
                            $myCalendar2->setPath("calendar/");
                            $myCalendar2->setYearInterval(2010, 2015);
                            $myCalendar2->dateAllow('2010-01-01', '2015-12-31');
                            //$myCalendar1->setHeight(350);
                            //$myCalendar1->autoSubmit(true, "form1");
                            //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
                            $myCalendar2->setOnChange("myChanged('test')");
                            //$myCalendar->rtl = true;
                            $myCalendar2->writeScript();
                            ?>	
      <!--  >input type="hidden" name="totime" value="<?php echo $myCalendar2->getDate(); ?>" />
      <input type="hidden" name="fromtime" value="<?php echo $myCalendar1->getDate(); ?>" /-->

                        </td>	
                        <td><input style="margin:12px 5px 0 0; font-size: 12px;" type="button" value="<?= $l_search ?>" onclick="sortMoney(1,'l.time',0,'search')" class="btn_choose"  /></td>											
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align: center;">
                            <input style="margin:2px 5px 0 0; font-size: 12px;" type="button" onclick="sortMoney(1,'l.time',0,'searchToday')" name="all" value="<?= $l_today ?>" class="btn_choose" />
                            <input style="margin:2px 5px 0 0; font-size: 12px;" type="button" onclick="sortMoney(1,'l.time',0,'searchLastweek')" name="all" value="<?= $l_lastweek ?>" class="btn_choose" />
                            <input style="margin:2px 5px 0 0; font-size: 12px;" type="button" onclick="sortMoney(1,'l.time',0,'searchAll')" name="all" value="<?= $l_all ?>" class="btn_choose" />
                        </td>
                    </tr>
                </table>



                <div  style="width:100%;padding: 10px 0;" id="frmLogMoney">
                    <table cellpadding="0" cellspacing="1" border="0" class="ftable1" style="padding-left: 10px">
                        <tr>
                            <th style="width: 30px"><?= $l_no ?></th>
                            <!--  <th style="width: 130px"><a onclick="sortMoney(<?= $page ?>,'l.type_trade','1','page')"><?php echo $l_nhap . '/' . $l_xuat; ?></a></th>-->
                            <th style="width: 200px"><a onclick="sortMoney(<?= $page ?>,'l.time','1','page')"><?= $l_time ?></a></th>
                            <th style="width: 130px"><a onclick="sortMoney(<?= $page ?>,'l.type_money','1','page')"><?= $l_tMoney ?></a></th>
                            <th style="width: 200px"><a onclick="sortMoney(<?= $page ?>,'l.value','1','page')"><?= $l_money ?></a></th>
                            <th style="width: 300px"><a onclick="sortMoney(<?= $page ?>,'e.FullName','1','page')"><?= $l_create_by ?></a></th>
                            <th style="width: 300px"><?= $l_note ?></th>
                        </tr>
                        <?php
                        $i = 0;
                        while ($r = mysql_fetch_array($logmoney)) {
                            if ($i >= $page_start) {
                                ?>
                                <tr>
                                    <td style="width: 30px"><?= $i + 1 ?></td>
                                    <!-- <td style="width: 130px"><?php
                        if ($r['type_trade'] == 0)
                            echo $l_nhap; else
                            echo $l_xuat;
                        ?></td>-->
                                    <td style="width: 200px"><?php echo $r['time']; ?></td>
                                    <td style="width: 130px"><?php
                                if ($r['type_money'] == 0)
                                    echo $l_tk1; else
                                    echo $l_tk2;
                                ?></td>
                                    <td style="width: 200px"><?php echo number_format($r['value']) . " VNĐ "; ?></td>
                                    <td style="width: 300px"><?php echo $r['FullName']; ?></td>
                                    <td style="width: 300px"><?php echo $r['note']; ?></td>
                                </tr>
                                <?php
                            }
                            $i++;
                            if ($i >= $page_end) {
                                break;
                            }
                        }
                        ?>	

                        <tr>
                            <td colspan="6" class="bt_td_main">

                                <span style="background:#fff; padding:1px; display: inline-block;">

                                    <?php
                                    if ($number_of_page > 1) {
                                        if ($page == 1)
                                            $p = 2;
                                        else if ($page == $number_of_page)
                                            $p = $number_of_page - 1;
                                        else
                                            $p=$page;
                                        if ($page != 1) {
                                            ?>
                                            <a onclick="sortMoney(1,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo "<<<"; ?></span> </a>
                                            <a onclick="sortMoney(<?= ($page - 1) ?>,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo "<<"; ?></span> </a>
                                            <?php
                                        } else {
                                            echo "<span class='button_next'><<<</span>";
                                            echo "<span class='button_next'><<</span>";
                                        }
                                        for ($i = $p - 1; $i <= $p + 1; $i++) {
                                            if ($i <= $number_of_page && $i > 0)
                                                if ($i == $page) {
                                                    echo "<span class='button_next'><b>" . $i . "</b></span>";
                                                } else {
                                                    ?>
                                                    <a onclick="sortMoney(<?= $i ?>,'<?= $str ?>',0,'page')" ><span class="button_next" style="color: #508e21;">
                                                    <?php
                                                    echo $i;
                                                    ?>
                                                        </span> </a>
                                                    <?php
                                                }
                                            ?>

                                            <?php
                                        }

                                        if ($page != $number_of_page) {
                                            ?>
                                            <a onclick="sortMoney(<?= ($page + 1) ?>,'<?= $str ?>',0,'page')"><span class='button_next'><?php echo ">>"; ?></span> </a>
                                            <a onclick="sortMoney(<?= ($number_of_page) ?>,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo ">>>"; ?></span> </a>
                                            <?php
                                        } else {
                                            echo "<span class='button_next'>>></span> ";
                                            echo "<span class='button_next'>>>></span> ";
                                        }
                                    }
                                    ?>

                                </span>
                            </td>
                        </tr>
                    </table>

                </div>
            </div>
        </div>

    </div>


</div>
<?php mysql_close($connection); ?>